import base64
from functools import wraps
from basic.basic_driver import DRIVER
from const import SCREEN_PATH


def exception_handling(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        DRIVER.start_recording_screen()
        try:
            func(*args, **kwargs)
        except Exception as e:
            result = DRIVER.stop_recording_screen()
            screen = base64.b64decode(result)
            with open(f'{SCREEN_PATH}/{func.__name__}.mp4', 'wb') as f:
                f.write(screen)
            raise e
    return wrapper